package com.yikaiye.android.yikaiye.ui.activity.mine;

import android.view.WindowManager;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.databinding.ActivityAccountMergeBinding;
import com.yikaiye.android.yikaiye.dialog.MyLoadingDialog;
import com.yikaiye.android.yikaiye.dialog.VerifyPhoneNumberDialog;
import com.yikaiye.android.yikaiye.model.UserModel;
import com.yikaiye.android.yikaiye.ui.adapter.MergeAccountAdapter;
import com.yikaiye.android.yikaiye.ui.fragment.viewmodel.MineViewModel;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.ToastHelper;
import com.yikaiye.android.yikaiye.widget.CountDownTextView;

import java.util.List;

/**
 * 账号合并
 */
public class AccountMergeActivity extends BaseActivity<ActivityAccountMergeBinding, MineViewModel> {

    private MyLoadingDialog loadingDialog;
    private MergeAccountAdapter adapter;
    private UserModel.MergeTelModel mMergeTelModel;
    private int mPosition;
    private CountDownTextView mCodeTv;

    @Override
    protected MineViewModel bindModel() {
        return getViewModel(this, MineViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.account_merge));
        adapter = new MergeAccountAdapter(context);
        binding.recyclerView.setAdapter(adapter);
        loadingDialog = new MyLoadingDialog(context, getString(R.string.account_merge_loading));
        loadingDialog.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        viewModel.getUserInfo();
    }

    @Override
    protected void initClick() {
        // 账号列表
        viewModel.userModelMutableLiveData.observe(this, userModel -> {
            if (userModel != null) {
                List<UserModel.MergeTelModel> telModels = userModel.getMergeTels();
                // 第一个为当前默认手机号，不用显示
                telModels.remove(0);
                adapter.submitList(telModels);
                StringBuilder tels = new StringBuilder();
                for (int i = 0; i < telModels.size(); i++) {
                    tels.append("\"").append(CommonUtil.getEncryptPhone(telModels.get(i).getTelNumber())).append("\"").append(i == telModels.size() - 1 ? "" : "、");
                }
                binding.contentTv.setText(getString(R.string.merge_account_tips, tels.toString()));
            }
        });

        // 验证码发送成功
        viewModel.sendCodeSuccess.observe(this, s -> {
            mCodeTv.startCountDown(context, 60, getString(R.string.get_security_code), getString(R.string.send_again));
            ToastHelper.show(getString(R.string.code_has_been_sent));
        });

        // 账号合并成功
        viewModel.mergeSuccess.observe(this, s -> {
            mMergeTelModel.setMappingStatus(1);
            adapter.getItems().set(mPosition, mMergeTelModel);
            adapter.notifyItemChanged(mPosition);
            loadingDialog.dismiss();
        });

        // 账号合并失败
        viewModel.mergeError.observe(this, s -> {
            ToastHelper.show(s);
            loadingDialog.dismiss();
        });

        adapter.setOnMergeClickListener((phone, model, position) -> {
            mMergeTelModel = model;
            mPosition = position;
            VerifyPhoneNumberDialog dialog = new VerifyPhoneNumberDialog(context, phone) {
                @Override
                public void verifyNow(String smsCode) { // 立即合并
                    loadingDialog.show();
                    viewModel.mergeAccount(phone, smsCode);
                }

                @Override
                public void getCode(CountDownTextView codeTv) { // 获取验证码
                    viewModel.sendSmsCode(phone);
                    mCodeTv = codeTv;
                }
            };
            dialog.show();
        });
    }
}